﻿using System;
using System.Web.Script.Serialization;
using System.Web.Services;
using RedHouse.Data.Base;
using RedHouse.Data.Service;
using RedHouse.Data.Entities;
using System.Collections.Generic;
using System.Globalization;
using RedHouse.Data.Common;

namespace RedHouse.AdminControl
{
    public partial class News : System.Web.UI.Page
    {
        public int Action { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Action"] != null)
                {
                    Action = int.Parse(Request.QueryString["Action"].ToString());
                }
                Initialize();
            }
        }

        private void Initialize()
        {

            switch (Action)
            {
                case 1:
                    NewsDetail.Visible = true;
                    NewsList.Visible = false;
                    break;
                case 2:
                    NewsDetail.Visible = false;
                    NewsList.Visible = true;
                    break;
                default:
                    break;
            }
        }

        [WebMethod]
        public static void DeleteNews(string id)
        {
            Guid newsId = new Guid(id);
            INewsService newsService = new NewsService();
            newsService.DeleteNews(newsId);
        }

        [WebMethod()]
        public static string EditNews(string id)
        {
            string jSonString = string.Empty;
            try
            {
                Guid newsId = new Guid(id);
                INewsService newsService = new NewsService();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                RedHouseNews news = newsService.GetNewsById(newsId);
                jSonString = serializer.Serialize(news);
            }
            catch (Exception ex)
            {
                return jSonString;
            }
            return jSonString;
        }

        [WebMethod()]
        public static string SaveNews(object news)
        {
            try
            {
                RedHouseNews redHouseNews = GetRedHouseNews(news);
                NewsService.Instance.UpdateNews(redHouseNews);
                return "save successfully!";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public static RedHouseNews GetRedHouseNews(object news)
        {
            RedHouseNews retNews = new RedHouseNews();
            Dictionary<string, object> tmp = (Dictionary<string, object>)news;
            object id = null;
            object location = null;
            object category = null;
            object lang = null;
            object des = null;
            object imageUrl = null;
            object title = null;
            object startDate = null;
            object endDate = null;

            tmp.TryGetValue("Id", out id);
            tmp.TryGetValue("Location", out location);
            tmp.TryGetValue("Category", out category);
            tmp.TryGetValue("LangId", out lang);
            tmp.TryGetValue("Description", out des);
            tmp.TryGetValue("ImageUrl", out imageUrl);
            tmp.TryGetValue("Title", out title);
            tmp.TryGetValue("StartDate", out startDate);
            tmp.TryGetValue("EndDate", out endDate);
            retNews.NewsDetail = new Data.Detail();
            retNews.NewsDetail.Title = title.ToString();
            retNews.Id = new Guid(id.ToString());
            retNews.NewsDetail.LangID = new Guid(lang.ToString());
            retNews.CategoryId = new Guid(category.ToString());
            retNews.LocationId = int.Parse(location.ToString());
            retNews.LangId = new Guid(lang.ToString());
            retNews.NewsDetail.Description= des.ToString();
            retNews.NewsDetail.ImageUrl = imageUrl.ToString();
            retNews.CreatedDate = DateTime.Now;
            retNews.CreatedBy = "admin";
            retNews.NewsGroup = RedHouseGroup.GroupList[0].Value;

            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "MM/dd/yyyy";
            dtfi.DateSeparator = "/";

            retNews.StartDate = DateTime.Parse(startDate.ToString(), dtfi);
            retNews.EndDate = DateTime.Parse(endDate.ToString(), dtfi);
            //retNews.DisplayOrder = int.Parse(order.ToString());
            return retNews;
        }
    }
}